var LayerZooms = {
    far: {trafficMaxZoom: 15.25, trafficMinZoom: 9},
    middle: {spriteMinZoom: 15.25, spriteMaxZoom: 17},
    tracking: {minZoom: 13, maxZoom: 17},
    near: {minZoom: 17}
}

var LayerManager = {
    addLayers: function (map) {
        //建筑物 从中景开始
        var names = ['building-1', 'building-2', 'building-3','building-4', 'building-5', 'building-6'];

        var myFilters = [
            ['all', ['>=', 'levels', 0], ['<', 'levels', 10]],
            ['all', ['>=', 'levels', 10], ['<', 'levels', 20]],
            ['all', ['>=', 'levels', 20], ['<', 'levels', 30]],
            ['all', ['>=', 'levels', 30], ['<', 'levels', 40]],
            ['all', ['>=', 'levels', 40], ['<', 'levels', 50]],
            ['>=', 'levels', 50]
        ]
        for (var i = 0; i < 6; i++) {
            map.addLayer({
                id: 'mybuilding' + i,
                type: "extrusion",
                source: 'Buildingmore',
                'source-layer': "Buildingmore",
                paint: {
                    "extrusion-color": "#30948f",
                    "extrusion-height": {"property": "levels", "type": "identity"},
                    "extrusion-pattern": names[i]
                },
                'minzoom': 14,
                'filter': myFilters[i]
            });
        }

        //添加高架桥

        map.addSource('source3d', {
            type: 'vector',
            tiles: ['http://datahive.minedata.cn:48085/test/roadpolygon/{z}/{x}/{y}']
        });

        map.addLayer({
            type: 'extrusion',
            id: '3d_no_shihua',
            source: 'source3d',
            'source-layer': 'RoadPolygon',
            paint: {
                'extrusion-color': 'rgb(42,58,86)',
                "render-road": true
            },
            minzoom: 17,
            filter: ["all", ["!=", 'pid', 185310], ['!=', 'pid', 388950]]
        });

        map.addLayer({
            type: 'extrusion',
            id: '3d_shihua',
            source: 'source3d',
            'source-layer': 'RoadPolygon',
            paint: {
                'extrusion-color': 'rgb(58,62,71)',
                "render-road": true
            },
            minzoom: 17,
            filter: ["any", ["==", 'pid', 185310], ['==', 'pid', 388950]]
        });

        //道路标线
        var types = ['车道中心线_黄虚线',
            '车行道分界线_白色虚线69线',
            '停止线_30CM停止线',
            '车行道分界线_白色实线',
            '车行道边缘线_白色实线'];

        var colors = ['yellow', '#80a7b9', '#80a7b9', '#80a7b9', '#80a7b9'];

        var dashes = [
            [10, 10],
            [12, 18],
            null,
            null,
            null
        ];

        map.addSource('lineSource', {
            type: 'vector',
            tiles: ['http://datahive.minedata.cn:48085/test/roadline/{z}/{x}/{y}']
        })

        for (var i = 0; i < types.length; i++) {

            var layer = {
                "id": "road_line_" + i,
                "type": "line",
                "source": "lineSource",
                "source-layer": "RoadLine",
                "layout": {
                    "line-join": "round",
                    "line-cap": "round",
                    "visibility": "visible"
                },
                "paint": {
                    "render-3d": true,
                    "line-opacity": 1,
                    "line-color": colors[i],
                    "line-width": {stops: [[10, 0.8], [17, 1]], base: 1.2},
                    "line-dasharray": dashes[i]
                },
                'minzoom': LayerZooms.near.minZoom,
                filter: ['==', 'type', types[i]]
            }

            if (!dashes[i]) {
                delete  layer.paint['line-dasharray'];
            }

            map.addLayer(layer);
        }

        /**
         * 这个就是动态图层，启用这个的时候首先要 把map.repaint = true写了
         */

        map.addSource('carSource', {
            type: 'vector',
            tiles: ["http://datahive.minedata.cn:48085/test/CarTrack3D/{z}/{x}/{y}"]
        });

        map.addLayer({
            "type": "symtracking",
            "source": 'carSource',
            "source-layer": "CarTrack3D",
            "id": 'carLayer',
            "layout": {
                "icon-image": "huhuai_car1",
                "icon-allow-overlap": true,
                "icon-ignore-placement": true,
                "symbol-avoid-edges": false,
                "icon-rotation-alignment": "map",
                "symtracking-fps": 10,             //行动快慢，现在是1秒1帧
                "symtracking-time-segment": 86, //一批数据中总的行动点数
                "compatible-mode": true //采用新的数据处理方式，精简流畅
            },
            "paint": {
                "icon-color": "#ff4089",
                "render-3d": true,
                "symtracking-height-offset": 3
            },
            'minzoom': LayerZooms.near.minZoom

        });

        map.addSource("trackingline", {
            'type': 'vector',
            'tiles': ['http://datahive.minedata.cn:48085/test/TrackLine3D/{z}/{x}/{y}']
        })

        map.addLayer({
            "id": "trackinglines1",
            "type": "line",
            "source": "trackingline",
            "source-layer": "TrackLine3D",
            "layout": {
                "line-join": "bevel",
                "line-cap": "butt"
            },
            "paint": {
                "line-color": "#991122",
                "line-width": 10,
                'line-opacity': 0.2,
                'render-3d': true,
                "line-height-offset": 110
            },
            minzoom: LayerZooms.tracking.minZoom,
            maxzoom: LayerZooms.tracking.maxZoom,
        })


        map.addSource("tracking", {
            'type': 'vector',
            'tiles': ['http://datahive.minedata.cn:48085/test/TrackCars3D/{z}/{x}/{y}']
        })

        map.addLayer({
            "id": "lines1",
            "type": "tracking",
            "source": "tracking",
            "source-layer": "TrackCars3D",
            "layout": {
                "tracking-join": "round",
                "tracking-cap": "round"
            },
            "paint": {
                'tracking-type': 'history',
                'tracking-seg-count': 700,
                'tracking-speed': 20,
                "tracking-color": "#991122",
                "tracking-width": 20,
                'tracking-opacity': 0.05,
                'render-3d': true,
                "tracking-height-offset": 110
            },
            minzoom: LayerZooms.tracking.minZoom,
            maxzoom: LayerZooms.tracking.maxZoom,
        })
        map.addLayer({
            "id": "lines",
            "type": "tracking",
            "source": "tracking",
            "source-layer": "TrackCars3D",
            "layout": {
                "tracking-join": "bevel",
                "tracking-cap": "butt"
            },
            "paint": {
                'tracking-type': 'history',
                'tracking-seg-count': 700,
                'tracking-speed': 20,
                "tracking-color": "#991122",
                "tracking-width": 3,
                'tracking-opacity': 0.6,
                'render-3d': true,
                "tracking-height-offset": 110
            },
            minzoom: LayerZooms.tracking.minZoom,
            maxzoom: LayerZooms.tracking.maxZoom,
        })

        //添加两个event gif图

        for (var i = 0; i < 10; i++) {
            map.addSource("overlay" + i, {
                "type": "image",
                "url": "texture/giftest/" + (i + 1) + ".png",
                "coordinates": [
                    [116.4357714405884, 39.9632456401249], [116.43107829541846, 39.95964826404879], [116.4357714405884, 39.9560510772151], [116.44046458575833, 39.95964826404879]
                ]
            });
            map.addLayer({
                "id": "imageLayer" + i,
                "type": "raster",
                "source": "overlay" + i,
                "minzoom": LayerZooms.far.trafficMinZoom,
                "maxzoom": 16,
                "layout": {
                    "visibility": "visible"
                },
                "paint": {
                    "raster-opacity": 0.85,
                    "render-carousel": true,
                    "raster-seq": i,
                    "raster-total-img": 10,
                    'raster-opacity-transition': {
                        duration: 0
                    }
                }
            })

            map.addSource("event" + i, {
                "type": "image",
                "url": "texture/giftest/" + (i + 1) + ".png",
                "coordinates": [
                    [116.43870356507195, 39.95302667784014], [116.43401112108171, 39.94942930179831], [116.43870356507195, 39.945832114930354], [116.4433960090622, 39.94942930179831]
                ]
            });
            map.addLayer({
                "id": "eventLayer" + i,
                "type": "raster",
                "source": "event" + i,
                "minzoom": LayerZooms.far.trafficMinZoom,
                "maxzoom": 16,
                "layout": {
                    "visibility": "visible"
                },
                "paint": {
                    "raster-opacity": 0.85,
                    "render-carousel": true,
                    "raster-seq": i,
                    "raster-total-img": 10,
                    'raster-opacity-transition': {
                        duration: 0
                    }
                }
            })

            map.addSource("shihua" + i, {
                "type": "image",
                "url": "texture/giftest/gifshihua/" + (i + 1) + ".png",
                "coordinates": [[116.43388835074823, 39.95382432958004], [116.42919585203958, 39.95022695353552], [116.43388835074823, 39.946629766670235], [116.43858084945686, 39.95022695353552]]
            });
            map.addLayer({
                "id": "shihuaLayer" + i,
                "type": "raster",
                "source": "shihua" + i,
                "minzoom": LayerZooms.far.trafficMinZoom,
                "maxzoom": 16,
                "layout": {
                    "visibility": "visible"
                },
                "paint": {
                    "raster-opacity": 0.85,
                    "render-carousel": true,
                    "raster-seq": i,
                    "raster-total-img": 10,
                    'raster-opacity-transition': {
                        duration: 0
                    }
                }
            })

            map.addSource("other1" + i, {
                "type": "image",
                "url": "texture/giftest/" + (i + 1) + ".png",
                "coordinates": [[116.48042001835842, 39.95878895790783], [116.4757271790303, 39.95519158184666], [116.48042001835842, 39.951594394998025], [116.48511285768656, 39.95519158184666]]
            });
            map.addLayer({
                "id": "other1Layer" + i,
                "type": "raster",
                "source": "other1" + i,
                "minzoom": LayerZooms.far.trafficMinZoom,
                "maxzoom": 16,
                "layout": {
                    "visibility": "visible"
                },
                "paint": {
                    "raster-opacity": 0.85,
                    "render-carousel": true,
                    "raster-seq": i,
                    "raster-total-img": 10,
                    'raster-opacity-transition': {
                        duration: 0
                    }
                }
            })

            map.addSource("other2" + i, {
                "type": "image",
                "url": "texture/giftest/" + (i + 1) + ".png",
                "coordinates": [[116.40248575320902, 39.95247251440996], [116.39779334723278, 39.948875138369985], [116.40248575320902, 39.945277951500174], [116.40717815918528, 39.948875138369985]]
            });
            map.addLayer({
                "id": "other2Layer" + i,
                "type": "raster",
                "source": "other2" + i,
                "minzoom": LayerZooms.far.trafficMinZoom,
                "maxzoom": 16,
                "layout": {
                    "visibility": "visible"
                },
                "paint": {
                    "raster-opacity": 0.85,
                    "render-carousel": true,
                    "raster-seq": i,
                    "raster-total-img": 10,
                    'raster-opacity-transition': {
                        duration: 0
                    }
                }
            })

            map.addSource("other3" + i, {
                "type": "image",
                "url": "texture/giftest/" + (i + 1) + ".png",
                "coordinates": [[116.42162599673088, 39.98049728336822], [116.41693166701548, 39.97689990723423], [116.42162599673088, 39.97330272045843], [116.42632032644627, 39.97689990723423]]
            });
            map.addLayer({
                "id": "other3Layer" + i,
                "type": "raster",
                "source": "other3" + i,
                "minzoom": LayerZooms.far.trafficMinZoom,
                "maxzoom": 16,
                "layout": {
                    "visibility": "visible"
                },
                "paint": {
                    "raster-opacity": 0.85,
                    "render-carousel": true,
                    "raster-seq": i,
                    "raster-total-img": 10,
                    'raster-opacity-transition': {
                        duration: 0
                    }
                }
            })
        }

        //添加两个事件点位
        map.addSource('myEventSource', {
            type: 'geojson',
            data: 'assets/events.json'
        })

        map.addLayer({
            "id": "eventMarkerTwo",
            "type": "symbol",
            "source": "myEventSource",
            "layout": {
                //"symbol-placement": "line",
                "icon-image": '{icon}',
                "text-offset": [0, 0.6],
                "icon-allow-overlap": true,  //图标允许压盖
            },
            "paint": {
                "render-3d": true,
                "render-road-mark": true,
                "symbol-height-offset": 5
            },
            filter: ["==", 'type', 0],
            'minzoom': LayerZooms.near.minZoom
        });

    }

}

export default LayerManager;

